package com.zrwl.poscloud.core.restapi.admin.baseInfo;
import com.zrwl.poscloud.base.enums.EAskforleave;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.TeacherAdminRService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.TeacherAdminRVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zrwl.poscloud.xo.service.AskforleaveService;
import com.zrwl.poscloud.xo.vo.AskforleaveVO;
/**
 * 请假管理 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-17
 */
@RestController
@Api(value = "请假管理相关接口", tags = {"请假管理相关接口"})
@RequestMapping("/askforleave")
@Slf4j
public class AskforleaveRestApi {
    @Autowired
    private AskforleaveService askforleaveService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private TeacherAdminRService teacherAdminRService;
    @AuthorityVerify
    @ApiOperation(value = "获取请假管理列表", notes = "获取请假管理列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody AskforleaveVO askforleaveVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取请假管理列表: {}", askforleaveVO);
        TeacherAdminRVO currentTeacher = teacherAdminRService.getCurrentTeacher();
        if(currentTeacher != null){
            askforleaveVO.setCheckEmployeeUid(currentTeacher.getEmployeeUid());
            askforleaveVO.setDeptRoleTree(currentTeacher.getCompanyCodeAll().split(","));
        }
        return ResultUtil.successWithData(askforleaveService.getPageList(askforleaveVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取请假管理列表", notes = "获取请假管理列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody AskforleaveVO askforleaveVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取请假管理列表: {}", askforleaveVO);
        return ResultUtil.successWithData(askforleaveService.getAllList(askforleaveVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增请假管理")    @ApiOperation(value = "新增请假管理", notes = "新增请假管理", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody AskforleaveVO askforleaveVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增请假管理: {}", askforleaveVO);
        askforleaveVO.setCompanyUid(adminService.getMe().getCompanyUid());
        return askforleaveService.add(askforleaveVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "修改请假管理")    @ApiOperation(value = "修改请假管理", notes = "修改请假管理", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody AskforleaveVO askforleaveVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改请假管理: {}", askforleaveVO);
        return askforleaveService.edit(askforleaveVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "假条审核")    @ApiOperation(value = "假条审核", notes = "假条审核", response = String.class)
    @PostMapping("/editAudit")
    public String editAudit(@Validated({Update.class}) @RequestBody AskforleaveVO askforleaveVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("假条审核: {}", askforleaveVO);
        TeacherAdminRVO currentTeacher = teacherAdminRService.getCurrentTeacher();
        if(currentTeacher != null){
            askforleaveVO.setCheckEmployeeUid(currentTeacher.getEmployeeUid());
        }
        askforleaveVO.setCompanyUid(adminService.getMe().getCompanyUid());
        return askforleaveService.editAudit(askforleaveVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "假条取消")    @ApiOperation(value = "假条取消", notes = "假条取消", response = String.class)
    @PostMapping("/editCancel")
    public String editCancel(@Validated({Update.class}) @RequestBody AskforleaveVO askforleaveVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("假条审核: {}", askforleaveVO);
        askforleaveVO.setCompanyUid(adminService.getMe().getCompanyUid());
        return askforleaveService.editCancel(askforleaveVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "删除请假管理")    @ApiOperation(value = "删除请假管理", notes = "删除请假管理", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody AskforleaveVO askforleaveVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除请假管理: {}", askforleaveVO);
        return askforleaveService.delete(askforleaveVO);
    }

}
